package yunhe.servlet;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import yunhe.biz.ISaleBiz;
import yunhe.biz.impl.SaleBizImpl;
import yunhe.entity.Sale;
import yunhe.entity.SaleAppend;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2017/6/20.
 */
@WebServlet(name = "SaleBackServlet",urlPatterns = "/sale/SaleBackServlet")
public class SaleBackServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ISaleBiz iSaleBizImpl = new SaleBizImpl();

        String cusId=request.getParameter("cusId");
        List<SaleAppend> list = iSaleBizImpl.saleListAndAppendAndProByCusId(cusId);
        JSONArray ja = new JSONArray();

        for (SaleAppend saleAppend : list) {
            JSONObject jbj = new JSONObject(saleAppend);
            try {
                jbj.put("salesId", saleAppend.getSale().getSalesId());
                jbj.put("saleDate", saleAppend.getSale().getSaleDate());
                jbj.put("cusId", saleAppend.getSale().getCustomer().getCusId());
                jbj.put("workId", saleAppend.getSale().getEmpMsg().getWorkId());
                jbj.put("proBarCode", saleAppend.getProduct().getProBarCode());
                jbj.put("saleQuantity", saleAppend.getSaleQuantity());
                jbj.put("salePrice", saleAppend.getSalePrice());
                jbj.put("saleDiscount", saleAppend.getSaleDiscount());
                jbj.put("saleTotal", saleAppend.getSaleTotal());
                jbj.put("excuteState", saleAppend.getSale().getExcuteState());

                System.out.println(jbj.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            ja.put(jbj);

        }
        response.getWriter().print(ja.toString());
    }
}
